package com.zj.fx.service;

import cn.hutool.core.util.StrUtil;
import com.zj.fx.common.base.BaseService;
import com.zj.fx.common.support.ConvertUtil;
import com.zj.fx.mapper.auto.SysDepartmentMapper;
import com.zj.fx.model.auto.DataTree;
import com.zj.fx.model.auto.SysDepartment;
import com.zj.fx.model.auto.SysDepartmentExample;
import com.zj.fx.model.custom.Tablepar;
import com.zj.fx.util.DateUtils;
import com.zj.fx.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 部门表 SysDepartmentService
 *
 * @author fuce_自动生成
 * @Title: SysDepartmentService.java
 * @Package com.zj.fx.service
 * @email 115889198@qq.com
 * @date 2020-04-17 13:12:58
 **/
@Service
@RequiredArgsConstructor
public class SysDepartmentService implements BaseService<SysDepartment, SysDepartmentExample> {
    private final SysDepartmentMapper sysDepartmentMapper;

    /**
     * 分页查询
     *
     * @return
     */
    public PageInfo<SysDepartment> list(Tablepar tablepar, String name) {
        SysDepartmentExample testExample = new SysDepartmentExample();
        testExample.setOrderByClause("id ASC");
        if (name != null && !"".equals(name)) {
            testExample.createCriteria().andDeptNameLike("%" + name + "%");
        }
        if (StrUtil.isNotEmpty(tablepar.getOrderByColumn())) {
            testExample.setOrderByClause(StringUtils.toUnderScoreCase(tablepar.getOrderByColumn()) + " " + tablepar.getIsAsc());
        }
        PageHelper.startPage(tablepar.getPage(), tablepar.getLimit());
        List<SysDepartment> list = sysDepartmentMapper.selectByExample(testExample);
        PageInfo<SysDepartment> pageInfo = new PageInfo<SysDepartment>(list);
        return pageInfo;
    }

    @Override
    public int deleteByPrimaryKey(String ids) {
        Integer[] integers = ConvertUtil.toIntArray(",", ids);
        List<Integer> stringB = Arrays.asList(integers);
        SysDepartmentExample example = new SysDepartmentExample();
        example.createCriteria().andIdIn(stringB);
        return sysDepartmentMapper.deleteByExample(example);

    }

    @Override
    public SysDepartment selectByPrimaryKey(String id) {

        Integer id1 = Integer.valueOf(id);
        return sysDepartmentMapper.selectByPrimaryKey(id1);

    }

    @Override
    public int updateByPrimaryKeySelective(SysDepartment record) {
        //判断父id等于null赋值0
        if (StringUtils.isEmpty(record.getParentId())) {
            record.setParentId("1");
        }
        return sysDepartmentMapper.updateByPrimaryKeySelective(record);
    }

    /**
     * 添加
     */
    @Override
    public int insertSelective(SysDepartment record) {
        record.setId(null);
        //判断父id等于null赋值0
        if (StringUtils.isEmpty(record.getParentId())) {
            record.setParentId("1");
        }
        record.setMonth(DateUtils.dateTimeNow(DateUtils.YYYY_MM));

        return sysDepartmentMapper.insertSelective(record);
    }

    @Override
    public int updateByExampleSelective(SysDepartment record, SysDepartmentExample example) {

        return sysDepartmentMapper.updateByExampleSelective(record, example);
    }

    @Override
    public int updateByExample(SysDepartment record, SysDepartmentExample example) {

        return sysDepartmentMapper.updateByExample(record, example);
    }

    @Override
    public List<SysDepartment> selectByExample(SysDepartmentExample example) {
        return sysDepartmentMapper.selectByExample(example);
    }

    public List<SysDepartment> selectByExample2(SysDepartment example) {
        return sysDepartmentMapper.selectByExample2(example);
    }

    @Override
    public long countByExample(SysDepartmentExample example) {

        return sysDepartmentMapper.countByExample(example);
    }

    @Override
    public int deleteByExample(SysDepartmentExample example) {

        return sysDepartmentMapper.deleteByExample(example);
    }

    /**
     * 检查name
     *
     * @param sysDepartment
     * @return
     */
    public int checkNameUnique(SysDepartment sysDepartment) {
        SysDepartmentExample example = new SysDepartmentExample();
        example.createCriteria().andDeptNameEqualTo(sysDepartment.getDeptName());
        List<SysDepartment> list = sysDepartmentMapper.selectByExample(example);
        return list.size();
    }

    /**
     * 修改权限状态展示或者不展示
     *
     * @param record
     * @return
     */
    public int updateVisible(SysDepartment record) {
        return sysDepartmentMapper.updateByPrimaryKeySelective(record);
    }

    public List<SysDepartment> query(String month) {
        return sysDepartmentMapper.query(month);
    }


}
